home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / unzip42.zip / FILE_IO.C < prev    next >
C/C++ Source or Header  |  1992-03-19  |  30KB  |  1,003 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   file_io.c
  4.  
  5.   This file contains routines for doing direct input/output, file-related
  6.   sorts of things.  Most of the system-specific code for unzip is contained
  7.   here, including the non-echoing password code for decryption (bottom).
  8.  
  9.   ---------------------------------------------------------------------------*/
  10.  
  11.  
  12. #define const
  13. #include "unzip.h"
  14.  
  15.  
  16. /************************************/
  17. /*  File_IO Local Prototypes, etc.  */
  18. /************************************/
  19.  
  20. #ifndef DOS_OS2
  21.    static int dos2unix __((unsigned char *buf, int len));
  22.    int CR_flag = 0;      /* when last char of buffer == CR (for dos2unix()) */
  23. #endif
  24.  
  25. #ifdef OS2
  26.    extern int   longname;          /* set in mapname.c */
  27.    extern char  longfilename[];
  28. #endif
  29.  
  30. #ifdef CRYPT
  31.    typedef char  voidp;
  32. #  if defined(DOS_OS2) || defined(VMS)
  33. #    define MSVMS
  34. #    ifdef DOS_OS2
  35. #      include <conio.h>
  36. #    else /* !DOS_OS2 */
  37. #      define getch() getc(stderr)
  38. #      define OFF 0   /* for echo control */
  39. #      define ON 1
  40. #      include <descrip.h>
  41. #      include <iodef.h>
  42. #      include <ttdef.h>
  43. #      ifndef SS$_NORMAL
  44. #        define SS$_NORMAL 1   /* only thing we need from <ssdef.h> */
  45. #      endif
  46. #    endif /* ?DOS_OS2 */
  47. #  else /* !(DOS_OS2 || VMS) */
  48. #    ifdef TERMIO    /* Amdahl, Cray, all SysV? */
  49. #      ifdef CONVEX
  50. #        include <sys/termios.h>
  51. #     include <sgtty.h>
  52. #      else /* !CONVEX */
  53. #        include <sys/termio.h>
  54. #        define sgttyb termio
  55. #        define sg_flags c_lflag
  56. #      endif /* ?CONVEX */
  57.        int ioctl OF((int, int, voidp *));
  58. #      define GTTY(f,s) ioctl(f,TCGETA,(voidp *)s)
  59. #      define STTY(f,s) ioctl(f,TCSETAW,(voidp *)s)
  60. #    else /* !TERMIO */
  61. #      ifndef MINIX
  62. #     include <sys/ioctl.h>
  63. #      endif /* !MINIX */
  64. #      include <sgtty.h>
  65.        int gtty OF((int, struct sgttyb *));
  66.        int stty OF((int, struct sgttyb *));
  67. #      define GTTY gtty
  68. #      define STTY stty
  69. #    endif /* ?TERMIO */
  70.      int isatty OF((int));
  71.      char *ttyname OF((int));
  72.      int open OF((char *, int, ...));
  73.      int close OF((int));
  74.      int read OF((int, voidp *, int));
  75. #  endif /* ?(DOS_OS2 || VMS) */
  76. #endif /* CRYPT */
  77.  
  78.  
  79.  
  80.  
  81.  
  82. /******************************/
  83. /* Function open_input_file() */
  84. /******************************/
  85.  
  86. int open_input_file()
  87. {                               /* return non-0 if open failed */
  88.     /*
  89.      *  open the zipfile for reading and in BINARY mode to prevent cr/lf
  90.      *  translation, which would corrupt the bitstreams
  91.      */
  92.  
  93. #ifdef UNIX
  94.     zipfd = open(zipfn, O_RDONLY);
  95. #else
  96.     zipfd = open(zipfn, O_RDONLY | O_BINARY);
  97. #endif
  98.     if (zipfd < 1) {
  99.         fprintf(stderr, "error:  can't open zipfile [ %s ]\n", zipfn);
  100.         return (1);
  101.     }
  102.     return 0;
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109. /**********************/
  110. /* Function readbuf() */
  111. /**********************/
  112.  
  113. int readbuf(buf, size)
  114. char *buf;
  115. register unsigned size;
  116. {                               /* return number of bytes read into buf */
  117.     register int count;
  118.     int n;
  119.  
  120.     n = size;
  121.     while (size) {
  122.         if (incnt == 0) {
  123.             if ((incnt = read(zipfd, (char *)inbuf, INBUFSIZ)) <= 0)
  124.                 return (n-size);
  125.             /* buffer ALWAYS starts on a block boundary:  */
  126.             cur_zipfile_bufstart += INBUFSIZ;
  127.             inptr = inbuf;
  128.         }
  129.         count = min(size, incnt);
  130.         memcpy(buf, inptr, count);
  131.         buf += count;
  132.         inptr += count;
  133.         incnt -= count;
  134.         size -= count;
  135.     }
  136.     return (n);
  137. }
  138.  
  139.  
  140.  
  141.  
  142.  
  143. #ifndef VMS   /* for VMS use code in vms.c (old VMS code below is retained
  144.                * in case of problems...will be removed in a later release) */
  145.  
  146. /*********************************/
  147. /* Function create_output_file() */
  148. /*********************************/
  149.  
  150. int create_output_file()
  151. {                               /* return non-0 if creat failed */
  152.  
  153.  
  154. /*---------------------------------------------------------------------------
  155.     Create the output file with appropriate permissions.  If we've gotten to
  156.     this point and the file still exists, we have permission to blow it away.
  157.   ---------------------------------------------------------------------------*/
  158.  
  159. #ifndef DOS_OS2
  160.     CR_flag = 0;   /* hack to get CR at end of buffer working */
  161. #endif
  162.  
  163. #if defined(UNIX) && !defined(AMIGA)
  164.     {
  165.         int mask;
  166.  
  167. #ifndef VMS
  168.         if (!stat(filename, &statbuf) && (unlink(filename) < 0)) {
  169.             fprintf(stderr, "\n%s:  cannot delete old copy\n", filename);
  170.             return 1;
  171.         }
  172. #  define EXTRA_ARGS
  173. #else /* VMS */
  174. #  define EXTRA_ARGS   ,"rfm=stmlf","rat=cr"
  175. #endif /* ?VMS */
  176.  
  177.         mask = umask(0);   /* now know we own it */
  178.         outfd = creat(filename, 0777 & pInfo->unix_attr  EXTRA_ARGS);
  179.         umask(mask);                                            /* VMS, Unix */
  180.     }
  181. #else /* !UNIX || AMIGA */  /* file permissions set after file closed */
  182. #if defined(MACOS) && defined(MPW) && !defined(MCH_MACINTOSH)
  183.     outfd = creat(filename);                         /* Mac MPW C compiler */
  184. #else /* !(Macintosh MPW 3.2 C) */
  185.     outfd = creat(filename, S_IWRITE | S_IREAD);     /* DOS, OS2, Mac, Amiga */
  186. #endif /* ?(Macintosh MPW 3.2 C) */
  187. #endif /* ?(UNIX && !AMIGA) */
  188.  
  189.     if (outfd < 1) {
  190.         fprintf(stderr, "\n%s:  cannot create\n", filename);
  191.         return 1;
  192.     }
  193.  
  194. /*---------------------------------------------------------------------------
  195.     If newly created file is in text mode and should be binary (to disable
  196.     automatic CR/LF translations), either close it and reopen as binary or
  197.     else change the mode to binary (DOS, OS/2).  If it is already binary and
  198.     should be text, change the mode to text (Mac).
  199.   ---------------------------------------------------------------------------*/
  200.  
  201. #ifndef UNIX
  202. #ifdef THINK_C
  203.     /*
  204.      * THINKC's stdio routines have the horrible habit of
  205.      * making any file you open look like generic files.
  206.      * This code tells the OS that it's a text file.
  207.      */
  208.     if (aflag) {
  209.         fileParam pb;
  210.         OSErr err;
  211.  
  212.         CtoPstr(filename);
  213.         pb.ioNamePtr = (StringPtr)filename;
  214.         pb.ioVRefNum = 0;
  215.         pb.ioFVersNum = 0;
  216.         pb.ioFDirIndex = 0;
  217.         err = PBGetFInfo(&pb,0);
  218.         if (err == noErr) {
  219.             pb.ioFlFndrInfo.fdCreator = '\?\?\?\?';
  220.             pb.ioFlFndrInfo.fdType = 'TEXT';
  221.             err = PBSetFInfo(&pb, 0);
  222.         }
  223.         PtoCstr(filename);
  224.     }
  225. #endif /* THINK_C */
  226.  
  227.     if (!aflag) {
  228. #ifdef DOS_OS2
  229.         if (setmode(outfd, O_BINARY) == -1) {
  230. #else /* !DOS_OS2 */
  231.         close(outfd);
  232.         if ((outfd = open(filename, O_RDWR | O_BINARY)) < 1) {
  233. #endif /* ?DOS_OS2 */
  234.             fprintf(stderr, "Can't make output file binary:  %s\n", filename);
  235.             return 1;
  236.         }
  237.     }
  238. #endif /* !UNIX */
  239.  
  240.     return 0;
  241. }
  242.  
  243. #endif /* !VMS */
  244.  
  245.  
  246.  
  247.  
  248.  
  249. /****************************/
  250. /* Function FillBitBuffer() */
  251. /****************************/
  252.  
  253. int FillBitBuffer()
  254. {
  255.     /*
  256.      * Fill bitbuf, which is 32 bits.  This function is only used by the
  257.      * READBIT and PEEKBIT macros (which are used by all of the uncompression
  258.      * routines).
  259.      */
  260.     UWORD temp;
  261.  
  262.     zipeof = 1;
  263.     while (bits_left < 25 && ReadByte(&temp) == 8)
  264.     {
  265.       bitbuf |= (ULONG)temp << bits_left;
  266.       bits_left += 8;
  267.       zipeof = 0;
  268.     }
  269.     return 0;
  270. }
  271.  
  272.  
  273.  
  274.  
  275.  
  276. /***********************/
  277. /* Function ReadByte() */
  278. /***********************/
  279.  
  280. int ReadByte(x)
  281. UWORD *x;
  282. {
  283.     /*
  284.      * read a byte; return 8 if byte available, 0 if not
  285.      */
  286.  
  287.  
  288.     if (csize-- <= 0)
  289.         return 0;
  290.  
  291.     if (incnt == 0) {
  292.         if ((incnt = read(zipfd, (char *)inbuf, INBUFSIZ)) <= 0)
  293.             return 0;
  294.         /* buffer ALWAYS starts on a block boundary:  */
  295.         cur_zipfile_bufstart += INBUFSIZ;
  296.         inptr = inbuf;
  297. #ifdef CRYPT
  298.         if (pInfo->encrypted) {
  299.             byte *p;
  300.             int n, t;
  301.  
  302.         for (n = incnt > csize + 1 ? (int)csize + 1 : incnt, p = inptr;
  303.                  n--; p++)
  304.                 *p = DECRYPT(*p);
  305.         }
  306. #endif /* CRYPT */
  307.     }
  308.     *x = *inptr++;
  309.     --incnt;
  310.     return 8;
  311. }
  312.  
  313.  
  314.  
  315.  
  316.  
  317. #ifndef VMS   /* for VMS use code in vms.c */
  318.  
  319. /**************************/
  320. /* Function FlushOutput() */
  321. /**************************/
  322.  
  323. int FlushOutput()
  324. {
  325.     /*
  326.      * flush contents of output buffer; return PK-type error code
  327.      */
  328. #ifndef DOS_OS2
  329. #   define CTRLZ  26
  330.     int saved_ctrlZ = FALSE;
  331. #endif /* !DOS_OS2 */
  332.     int len;
  333.  
  334.  
  335.     if (disk_full) {
  336.         outpos += outcnt;   /* fake emptied buffer */
  337.         outcnt = 0;
  338.         outptr = outbuf;
  339.         return 50;          /* ignore rest of this file */
  340.     }
  341.  
  342.     if (outcnt) {
  343.         UpdateCRC(outbuf, outcnt);
  344.  
  345.         if (!tflag) {
  346. #ifndef DOS_OS2
  347.             if (aflag) {
  348.                 if (outbuf[outcnt-1] == CTRLZ) {
  349.                     --outcnt;
  350.                     saved_ctrlZ = TRUE;
  351.                 }
  352.                 len = dos2unix(outbuf, outcnt);
  353.             } else
  354. #endif /* !DOS_OS2 */
  355.                 len = outcnt;
  356.             if (write(outfd, (char *)outout, len) != len)
  357. #ifdef DOS_OS2
  358.                 if (!cflag)           /* ^Z treated as EOF, removed with -c */
  359. #else /* !DOS_OS2 */
  360. #ifdef MINIX
  361.                 if (errno == EFBIG)
  362.                     if (write(fd, outout, len/2) != len/2  ||
  363.                         write(fd, outout+len/2, len/2) != len/2)
  364. #endif /* MINIX */
  365. #endif /* ?DOS_OS2 */
  366.                 {
  367.                     /* GRR: add test for force_flag when has its own switch */
  368.                     fprintf(stderr,
  369.                       "\n%s:  write error (disk full?).  Continue? (y/n/^C) ",
  370.                       filename);
  371.                     FFLUSH   /* for Amiga and Mac MPW */
  372.                     fgets(answerbuf, 9, stdin);
  373.                     if (*answerbuf == 'y')   /* stop writing to this file */
  374.                         disk_full = 1;       /*  (outfd bad?), but new OK */
  375.                     else
  376.                         disk_full = 2;       /* no:  exit program */
  377.                     return 50;    /* 50:  disk full */
  378.                 }
  379.         }
  380.         outpos += outcnt;
  381.         outcnt = 0;
  382.         outptr = outbuf;
  383. #ifndef DOS_OS2
  384.         if (saved_ctrlZ) {
  385.             *outptr++ = CTRLZ;
  386.             ++outcnt;
  387.         }
  388. #endif /* !DOS_OS2 */
  389.     }
  390.     return (0);                 /* 0:  no error */
  391. }
  392.  
  393. #endif /* !VMS */
  394.  
  395.  
  396.  
  397.  
  398.  
  399. #ifndef DOS_OS2   /* GRR:  rewrite this for generic text conversions */
  400.  
  401. /***********************/
  402. /* Function dos2unix() */
  403. /***********************/
  404.  
  405. static int dos2unix(buf, len)
  406. unsigned char *buf;
  407. int len;
  408. {
  409.     int new_len;
  410.     int i;
  411.     unsigned char *walker;
  412.  
  413.     new_len = len;
  414.     walker = outout;
  415. #ifdef MACOS
  416.     /*
  417.      * Mac wants to strip LFs instead CRs from CRLF pairs
  418.      */
  419.     if (CR_flag && *buf == LF) {
  420.         buf++;
  421.         new_len--;
  422.         len--;
  423.         CR_flag = buf[len] == CR;
  424.     }
  425.     else
  426.         CR_flag = buf[len - 1] == CR;
  427.     for (i = 0; i < len; i += 1) {
  428.         *walker++ = ascii_to_native(*buf);
  429.         if (*buf == LF) walker[-1] = CR;
  430.         if (*buf++ == CR && *buf == LF) {
  431.             new_len--;
  432.             buf++;
  433.             i++;
  434.         }
  435.     }
  436. #else
  437.     if (CR_flag && *buf != LF)
  438.         *walker++ = ascii_to_native(CR);
  439.     CR_flag = buf[len - 1] == CR;
  440.     for (i = 0; i < len; i += 1) {
  441.         *walker++ = ascii_to_native(*buf);
  442.         if (*buf++ == CR && *buf == LF) {
  443.             new_len--;
  444.             walker[-1] = ascii_to_native(*buf++);
  445.             i++;
  446.         }
  447.     }
  448.     /*
  449.      * If the last character is a CR, then "ignore it" for now...
  450.      */
  451.     if (walker[-1] == ascii_to_native(CR))
  452.         new_len--;
  453. #endif
  454.     return new_len;
  455. }
  456.  
  457. #endif /* !DOS_OS2 */
  458.  
  459.  
  460.  
  461.  
  462.  
  463. #ifdef DOS_OS2
  464.  
  465. /**************************************/
  466. /* Function set_file_time_and_close() */
  467. /**************************************/
  468.  
  469. void set_file_time_and_close()
  470.  /*
  471.   * MS-DOS AND OS/2 VERSION (Mac, Unix/VMS versions are below)
  472.   *
  473.   * Set the output file date/time stamp according to information from the
  474.   * zipfile directory record for this member, then close the file and set
  475.   * its permissions (archive, hidden, read-only, system).  Aside from closing
  476.   * the file, this routine is optional (but most compilers support it).
  477.   */
  478. {
  479. /*---------------------------------------------------------------------------
  480.     Allocate local variables needed by OS/2 and Turbo C.
  481.   ---------------------------------------------------------------------------*/
  482.  
  483. #ifdef OS2              /* (assuming only MSC or MSC-compatible compilers
  484.                          * for this part) */
  485.  
  486.     union {
  487.         FDATE fd;               /* system file date record */
  488.         UWORD zdate;            /* date word */
  489.     } ud;
  490.  
  491.     union {
  492.         FTIME ft;               /* system file time record */
  493.         UWORD ztime;            /* time word */
  494.     } ut;
  495.  
  496.     FILESTATUS fs;
  497.  
  498. #else                           /* !OS2 */
  499. #ifdef __TURBOC__
  500.  
  501.     union {
  502.         struct ftime ft;        /* system file time record */
  503.         struct {
  504.             UWORD ztime;        /* date and time words */
  505.             UWORD zdate;        /* .. same format as in .ZIP file */
  506.         } zt;
  507.     } td;
  508.  
  509. #endif                          /* __TURBOC__ */
  510. #endif                          /* !OS2 */
  511.  
  512. /*---------------------------------------------------------------------------
  513.      Do not attempt to set the time stamp on standard output.
  514.   ---------------------------------------------------------------------------*/
  515.  
  516.     if (cflag) {
  517.         close(outfd);
  518.         return;
  519.     }
  520.  
  521. /*---------------------------------------------------------------------------
  522.     Copy and/or convert time and date variables, if necessary; then set the
  523.     file time/date.
  524.   ---------------------------------------------------------------------------*/
  525.  
  526. #ifdef OS2
  527.     DosQFileInfo(outfd, 1, &fs, sizeof(fs));
  528.     ud.zdate = lrec.last_mod_file_date;
  529.     ut.ztime = lrec.last_mod_file_time;
  530.     fs.fdateLastWrite = ud.fd;
  531.     fs.ftimeLastWrite = ut.ft;
  532.     fs.attrFile = pInfo->dos_attr; /* hidden, system, archive, read-only */
  533.     DosSetFileInfo(outfd, 1, (PBYTE) &fs, sizeof(fs));
  534. #else /* !OS2 */
  535. #ifdef __TURBOC__
  536.     td.zt.ztime = lrec.last_mod_file_time;
  537.     td.zt.zdate = lrec.last_mod_file_date;
  538.     setftime(outfd, &td.ft);
  539. #else /* !__TURBOC__ */
  540.     _dos_setftime(outfd, lrec.last_mod_file_date, lrec.last_mod_file_time);
  541. #endif /* ?__TURBOC__ */
  542. #endif /* ?OS2 */
  543.  
  544. /*---------------------------------------------------------------------------
  545.     And finally we can close the file...at least everybody agrees on how to
  546.     do *this*.  I think...  Oh yeah, also change the mode according to the
  547.     stored file attributes, since we didn't do that when we opened the dude.
  548.   ---------------------------------------------------------------------------*/
  549.  
  550.     close(outfd);
  551.  
  552. #ifdef OS2
  553.     if (longname)
  554.         SetLongNameEA(filename, longfilename);
  555. #else /* !OS2 */
  556. #ifdef __TURBOC__
  557.     if (_chmod(filename, 1, pInfo->dos_attr) != pInfo->dos_attr)
  558.         fprintf(stderr, "\nwarning:  file attributes may not be correct\n");
  559. #else /* !__TURBOC__ */
  560.     _dos_setfileattr(filename, pInfo->dos_attr);
  561. #endif /* ?__TURBOC__ */
  562. #endif /* ?OS2 */
  563.  
  564. } /* end function set_file_time_and_close() (DOS, OS/2) */
  565.  
  566.  
  567.  
  568.  
  569.  
  570. #else                           /* !DOS_OS2 */
  571. #ifdef MACOS                    /* Mac */
  572.  
  573. /**************************************/
  574. /* Function set_file_time_and_close() */
  575. /**************************************/
  576.  
  577. void set_file_time_and_close()
  578.  /*
  579.   * MAC VERSION
  580.   */
  581. {
  582.     long m_time;
  583.     DateTimeRec dtr;
  584.     ParamBlockRec pbr;
  585.     OSErr err;
  586.  
  587.     if (outfd != 1) {
  588.         close(outfd);
  589.  
  590.         /*
  591.          * Macintosh bases all file modification times on the number of seconds
  592.          * elapsed since Jan 1, 1904, 00:00:00.  Therefore, to maintain
  593.          * compatibility with MS-DOS archives, which date from Jan 1, 1980,
  594.          * with NO relation to GMT, the following conversions must be made:
  595.          *      the Year (yr) must be incremented by 1980;
  596.          *      and converted to seconds using the Mac routine Date2Secs(),
  597.          *      almost similar in complexity to the Unix version :-)
  598.          *                                     J. Lee
  599.          */
  600.  
  601.         dtr.year = (((lrec.last_mod_file_date >> 9) & 0x7f) + 1980);
  602.         dtr.month = ((lrec.last_mod_file_date >> 5) & 0x0f);
  603.         dtr.day = (lrec.last_mod_file_date & 0x1f);
  604.  
  605.         dtr.hour = ((lrec.last_mod_file_time >> 11) & 0x1f);
  606.         dtr.minute = ((lrec.last_mod_file_time >> 5) & 0x3f);
  607.         dtr.second = ((lrec.last_mod_file_time & 0x1f) * 2);
  608.  
  609.         Date2Secs(&dtr, (unsigned long *)&m_time);
  610.         CtoPstr(filename);
  611.         pbr.fileParam.ioNamePtr = (StringPtr)filename;
  612.         pbr.fileParam.ioVRefNum = pbr.fileParam.ioFVersNum =
  613.           pbr.fileParam.ioFDirIndex = 0;
  614.         err = PBGetFInfo(&pbr, 0L);
  615.         pbr.fileParam.ioFlMdDat = pbr.fileParam.ioFlCrDat = m_time;
  616.         if (err == noErr)
  617.             err = PBSetFInfo(&pbr, 0L);
  618.         if (err != noErr)
  619.             printf("error:  can't set the time for %s\n", filename);
  620.  
  621.         /* set read-only perms if needed */
  622.         if (err != noErr && pInfo->unix_attr != 0)
  623.             err = SetFLock((ConstStr255Param)filename, 0);
  624.         PtoCstr(filename);
  625.     }
  626. }
  627.  
  628.  
  629.  
  630.  
  631.  
  632. #else                                /* !MACOS... */
  633. #if !defined(MTS) && !defined(VMS)   /* && !MTS (can't do) && !VMS: only one
  634.                                       * left is UNIX (for VMS use code in
  635.                                       * vms.c--old VMS code below is retained
  636.                                       * in case of problems and will be removed
  637.                                       * in a later release) */
  638.  
  639. /**************************************/
  640. /* Function set_file_time_and_close() */
  641. /**************************************/
  642.  
  643. void set_file_time_and_close()
  644.  /*
  645.   * UNIX AND VMS VERSION (MS-DOS & OS/2, Mac versions are above)
  646.   */
  647. {
  648.     static short yday[]={0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  649.     long m_time;
  650.     int yr, mo, dy, hh, mm, ss, leap, days=0;
  651. #ifdef VMS
  652. #   define YRBASE  0
  653.     char timbuf[24];
  654.     static char *month[] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN",
  655.                             "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
  656.     struct VMStimbuf {
  657.         char *actime;           /* VMS revision date, ASCII format */
  658.         char *modtime;          /* VMS creation date, ASCII format */
  659.     } ascii_times;
  660. #else /* !VMS */
  661.     struct utimbuf {
  662.         time_t actime;          /* new access time */
  663.         time_t modtime;         /* new modification time */
  664.     } tp;
  665. #ifdef AMIGA
  666. #   define YRBASE  1978         /* in AmigaDos, counting begins 01-Jan-1978 */
  667.     struct DateStamp myadate;
  668. /*  extern char *_TZ;   no longer used? */
  669. #else /* !AMIGA */
  670. #   define YRBASE  1970
  671. #ifdef BSD
  672.     static struct timeb tbp;
  673. #else /* !BSD */
  674.     extern long timezone;
  675. #endif /* ?BSD */
  676. #endif /* ?AMIGA */
  677. #endif /* ?VMS */
  678.  
  679.  
  680.     /*
  681.      * Close the file *before* setting its time under Unix, AmigaDos and VMS.
  682.      */
  683. #ifdef AMIGA
  684.     if (cflag)                  /* can't set time on stdout */
  685.         return;
  686.     close(outfd);
  687. #else /* !AMIGA */
  688.     close(outfd);
  689.     if (cflag)                  /* can't set time on stdout */
  690.         return;
  691. #endif /* ?AMIGA */
  692.  
  693.     /*
  694.      * These date conversions look a little weird, so I'll explain.
  695.      * UNIX bases all file modification times on the number of seconds
  696.      * elapsed since Jan 1, 1970, 00:00:00 GMT.  Therefore, to maintain
  697.      * compatibility with MS-DOS archives, which date from Jan 1, 1980,
  698.      * with NO relation to GMT, the following conversions must be made:
  699.      *      the Year (yr) must be incremented by 10;
  700.      *      the Date (dy) must be decremented by 1;
  701.      *      and the whole mess must be adjusted by TWO factors:
  702.      *          relationship to GMT (ie.,Pacific Time adds 8 hrs.),
  703.      *          and whether or not it is Daylight Savings Time.
  704.      * Also, the usual conversions must take place to account for leap years,
  705.      * etc.
  706.      *                                     C. Seaman
  707.      */
  708.  
  709.     /* dissect date */
  710.     yr = ((lrec.last_mod_file_date >> 9) & 0x7f) + (1980 - YRBASE);
  711.     mo = ((lrec.last_mod_file_date >> 5) & 0x0f) - 1;
  712.     dy = (lrec.last_mod_file_date & 0x1f) - 1;
  713.  
  714.     /* dissect time */
  715.     hh = (lrec.last_mod_file_time >> 11) & 0x1f;
  716.     mm = (lrec.last_mod_file_time >> 5) & 0x3f;
  717.     ss = (lrec.last_mod_file_time & 0x1f) * 2;
  718.  
  719. #ifdef VMS
  720.     sprintf(timbuf, "%02d-%3s-%04d %02d:%02d:%02d.00", dy+1, month[mo],
  721.       yr, hh, mm, ss);
  722.  
  723.     ascii_times.actime = timbuf;
  724.     ascii_times.modtime = timbuf;
  725.  
  726.     if ((mm = VMSmunch(filename, SET_TIMES, &ascii_times)) != RMS$_NMF)
  727.         fprintf(stderr, "error %d:  can't set the time for %s\n", mm, filename);
  728.  
  729. #else /* !VMS */
  730.     /* leap = # of leap years from BASE up to but not including current year */
  731.     leap = ((yr + YRBASE - 1) / 4);   /* leap year base factor */
  732.  
  733.     /* How many days from BASE to this year? (& add expired days this year) */
  734.     days = (yr * 365) + (leap - 492) + yday[mo];
  735.  
  736.     /* if year is a leap year and month is after February, add another day */
  737.     if ((mo > 1) && ((yr+YRBASE)%4 == 0) && ((yr+YRBASE) != 2100))
  738.         ++days;                 /* OK through 2199 */
  739.  
  740. #ifdef AMIGA
  741. /*  _TZ = getenv("TZ"); does Amiga not have TZ and tzset() after all? */
  742.     myadate.ds_Days   =   days+dy-2;   /* off by one? */
  743.     myadate.ds_Minute =   hh*60+mm;
  744.     myadate.ds_Tick   =   ss*TICKS_PER_SECOND;
  745.  
  746.     if (!(SetFileDate(filename, &myadate)))
  747.         fprintf(stderr, "error:  can't set the time for %s\n", filename);
  748.  
  749. #else /* !AMIGA */
  750.     /* convert date & time to seconds relative to 00:00:00, 01/01/YRBASE */
  751.     m_time = ((days + dy) * 86400) + (hh * 3600) + (mm * 60) + ss;
  752.  
  753. #ifdef BSD
  754.     ftime(&tbp);
  755.     m_time += tbp.timezone * 60L;
  756. #else /* !BSD */
  757.     tzset();                    /* set `timezone' */
  758.     m_time += timezone;         /* account for timezone differences */
  759. #endif /* ?BSD */
  760.  
  761.     if (localtime(&m_time)->tm_isdst)
  762.         m_time -= 60L * 60L;    /* adjust for daylight savings time */
  763.  
  764.     tp.actime = m_time;         /* set access time */
  765.     tp.modtime = m_time;        /* set modification time */
  766.  
  767.     /* set the time stamp on the file */
  768.     if (utime(filename, &tp))
  769.         fprintf(stderr, "error:  can't set the time for %s\n", filename);
  770. #endif /* ?AMIGA */
  771. #endif /* ?VMS */
  772. }
  773.  
  774. #endif                          /* !MTS && !VMS */
  775. #endif                          /* ?MACOS */
  776. #endif                          /* ?DOS_OS2 */
  777.  
  778.  
  779.  
  780.  
  781.  
  782. /*******************************/
  783. /*  Non-echoing password code  */
  784. /*******************************/
  785.  
  786. #ifdef EMX32
  787. #  undef DOS_OS2
  788. #  undef MSVMS
  789. #endif
  790.  
  791. #ifdef CRYPT
  792. #ifndef DOS_OS2
  793. #ifdef VMS
  794.  
  795. int echo(opt)
  796.     int opt;
  797. {
  798. /*---------------------------------------------------------------------------
  799.     Based on VMSmunch.c, which in turn was based on Joe Meadows' file.c code.
  800.   ---------------------------------------------------------------------------
  801.      * For VMS v5.x:
  802.      *   IO$_SENSEMODE/SETMODE info:  Programming, Vol. 7A, System Programming,
  803.      *     I/O User's: Part I, sec. 8.4.1.1, 8.4.3, 8.4.5, 8.6
  804.      *   sys$assign(), sys$qio() info:  Programming, Vol. 4B, System Services,
  805.      *     System Services Reference Manual, pp. sys-23, sys-379
  806.      *   fixed-length descriptor info:  Programming, Vol. 3, System Services,
  807.      *     Intro to System Routines, sec. 2.9.2
  808.      * GRR, 15 Aug 91
  809.   ---------------------------------------------------------------------------*/
  810.     static struct dsc$descriptor_s DevDesc =
  811.         {9, DSC$K_DTYPE_T, DSC$K_CLASS_S, "SYS$INPUT"};
  812.      /* {dsc$w_length, dsc$b_dtype, dsc$b_class, dsc$a_pointer}; */
  813.     static short           DevChan, iosb[4];
  814.     static long            i, status;
  815.     static unsigned long   oldmode[2], newmode[2];   /* each = 8 bytes */
  816.   
  817.  
  818. /*---------------------------------------------------------------------------
  819.     Assign a channel to standard input.
  820.   ---------------------------------------------------------------------------*/
  821.  
  822.     status = sys$assign(&DevDesc, &DevChan, 0, 0);
  823. #ifdef DEBUG
  824.     printf("echo:  sys$assign returns status = %ld\n", status);
  825. #endif /* DEBUG */
  826.     if (!(status & 1)) return(status);
  827.  
  828. /*---------------------------------------------------------------------------
  829.     Use sys$qio and the IO$_SENSEMODE function to determine the current tty
  830.     status (for password reading, could use IO$_READVBLK function instead,
  831.     but echo on/off will be more general).
  832.   ---------------------------------------------------------------------------*/
  833.  
  834.     status = sys$qio(0, DevChan, IO$_SENSEMODE, &iosb, 0, 0,
  835.                      oldmode, 8, 0, 0, 0, 0);
  836. #ifdef DEBUG
  837.     printf("echo:  sys$qio(IO$_SENSEMODE) returns status = %ld\n", status);
  838.     printf("echo:  sys$qio(IO$_SENSEMODE) returns iosb status = %d\n", 
  839.       iosb[0]);
  840. #endif /* DEBUG */
  841.     if (!(status & 1)) return(status);
  842.     status = iosb[0];
  843.     if (!(status & 1)) return(status);
  844.  
  845. /*---------------------------------------------------------------------------
  846.     Copy old mode into new-mode buffer, then modify to be either NOECHO or
  847.     ECHO (depending on function argument opt).
  848.   ---------------------------------------------------------------------------*/
  849.  
  850.     newmode[0] = oldmode[0];
  851.     newmode[1] = oldmode[1];
  852.     if (opt == OFF)
  853.         newmode[1] |= TT$M_NOECHO;                      /* set NOECHO bit */
  854.     else
  855.         newmode[1] &= ~((unsigned long) TT$M_NOECHO);   /* clear NOECHO bit */
  856. #ifdef DEBUG
  857.     printf("echo:  current terminal status = %lx\n", oldmode[1]);
  858.     printf("echo:  current echo status = %s\n",
  859.       (oldmode[1] & TT$M_NOECHO)? "noecho" : "echo");
  860.     printf("echo:  new terminal status = %lx\n", newmode[1]);
  861.     printf("echo:  new echo status = %s\n",
  862.       (newmode[1] & TT$M_NOECHO)? "noecho" : "echo");
  863. #endif /* DEBUG */
  864.  
  865. /*---------------------------------------------------------------------------
  866.     Use the IO$_SETMODE function to change the tty status.
  867.   ---------------------------------------------------------------------------*/
  868.  
  869.     status = sys$qio(0, DevChan, IO$_SETMODE, &iosb, 0, 0,
  870.                      newmode, 8, 0, 0, 0, 0);
  871. #ifdef DEBUG
  872.     printf("echo:  sys$qio(IO$_SETMODE) returns status = %ld\n", status);
  873.     printf("echo:  sys$qio(IO$_SETMODE) returns iosb status = %d\n", iosb[0]);
  874. #endif /* DEBUG */
  875.     if (!(status & 1)) return(status);
  876.     status = iosb[0];
  877.     if (!(status & 1)) return(status);
  878.  
  879. /*---------------------------------------------------------------------------
  880.     Deassign the sys$input channel by way of clean-up, then exit happily.
  881.   ---------------------------------------------------------------------------*/
  882.  
  883.     status = sys$dassgn(DevChan);
  884. #ifdef DEBUG
  885.     printf("echo:  sys$dassgn returns status = %ld\n\n", status);
  886. #endif /* DEBUG */
  887.     if (!(status & 1)) return(status);
  888.  
  889.     return SS$_NORMAL;   /* we be happy */
  890.  
  891. } /* end function echo() */
  892.  
  893.  
  894.  
  895.  
  896.  
  897. #else /* !VMS */
  898.  
  899. static int echofd = -1;    /* file descriptor whose echo is off */
  900.  
  901. void echoff(f)
  902. int f;            /* file descriptor to turn echo off on */
  903. /* Turn echo off for file descriptor f.  Assumes that f is a tty device. */
  904. {
  905.   struct sgttyb sg;    /* tty device structure */
  906.  
  907.   echofd = f;
  908.   GTTY(f, &sg);                    /* get settings */
  909.   sg.sg_flags &= ~ECHO;                /* turn echo off */
  910.   STTY(f, &sg);
  911. }
  912.  
  913.  
  914.  
  915. void echon()
  916. /* Turn echo back on for file descriptor echofd. */
  917. {
  918.   struct sgttyb sg;    /* tty device structure */
  919.  
  920.   if (echofd != -1)
  921.   {
  922.     GTTY(echofd, &sg);                /* get settings */
  923.     sg.sg_flags |= ECHO;            /* turn echo on */
  924.     STTY(echofd, &sg);
  925.     echofd = -1;
  926.   }
  927. }
  928.  
  929. #endif /* ?VMS */
  930. #endif /* !DOS_OS2 */
  931.  
  932.  
  933.  
  934.  
  935.  
  936. char *getp(m, p, n)
  937. char *m;        /* prompt for password */
  938. char *p;        /* return value: line input */
  939. int n;            /* bytes available in p[] */
  940. /* Get a password of length n-1 or less into *p using the prompt *m.
  941.    The entered password is not echoed.  Return p on success, NULL on
  942.    failure (can't get controlling tty). */
  943. {
  944.   char c;        /* one-byte buffer for read() to use */
  945.   int i;        /* number of characters input */
  946.   char *w;        /* warning on retry */
  947.  
  948. #ifndef DOS_OS2
  949. #ifdef VMS
  950.   echo(OFF);                                    /* turn echo off */
  951. #else /* !VMS */
  952.   int f;        /* file decsriptor for tty device */
  953.  
  954.   /* Turn off echo on tty */
  955.   if (!isatty(2))
  956.     return NULL;                /* error if not tty */
  957.   if ((f = open(ttyname(2), 0, 0)) == -1)
  958.     return NULL;
  959.   echoff(f);                    /* turn echo off */
  960. #endif /* ?VMS */
  961. #endif /* !DOS_OS2 */
  962.  
  963.   /* Get password */
  964.   w = "";
  965.   do {
  966. #ifdef VMS   /* bug:  VMS adds '\n' to NULL fputs (apparently) */
  967.     if (*w)
  968. #endif /* VMS */
  969.     fputs(w, stderr);                /* warning if back again */
  970.     fputs(m, stderr);                /* prompt */
  971.     fflush(stderr);
  972.     i = 0;
  973.     do {                    /* read line, keeping n */
  974. #ifdef MSVMS
  975.       if ((c = (char)getch()) == '\r')
  976.         c = '\n';
  977. #else /* !MSVMS */
  978.       read(f, &c, 1);
  979. #endif /* ?MSVMS */
  980.       if (i < n)
  981.     p[i++] = c;
  982.     } while (c != '\n');
  983.     putc('\n', stderr);  fflush(stderr);
  984.     w = "(line too long--try again)\n";
  985.   } while (p[i-1] != '\n');
  986.   p[i-1] = 0;                    /* terminate at newline */
  987.  
  988. #ifndef DOS_OS2
  989. #ifdef VMS
  990.   echo(ON);                                     /* turn echo back on */
  991. #else /* !VMS */
  992.   /* Turn echo back on */
  993.   echon();                    /* turn echo back on */
  994.   close(f);
  995. #endif /* ?VMS */
  996. #endif /* !DOS_OS2 */
  997.  
  998.   /* Return pointer to password */
  999.   return p;
  1000. }
  1001.  
  1002. #endif /* CRYPT */
  1003.